package com.keyancompany.zhongbaopingtai.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.AppManager;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.ZBApplication;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.listener.LoadingHanlder;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.utils.KeyBoardUtils;
import com.keyancompany.zhongbaopingtai.utils.NetUtils;
import com.keyancompany.zhongbaopingtai.utils.ScreenUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import butterknife.ButterKnife;


/**
 * Created by Ken on 2015/3/13.
 */
public abstract class BaseActivity extends AppCompatActivity implements
        IAsyncHttpResponseHandler {

    private static final String TAG = "BaseActivity";

    protected Context mContext;

    private LoadingHanlder handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        addAppManager();

        setContentView(getContentViewId()); //获取布局文件

        ButterKnife.bind(this);

        setUpViews();
        initToolBar();

    }

    void addAppManager() {
        AppManager.getAppManager().addActivity(this); //app堆栈管理 方便 退出营运
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    void removeAppManager() {
        AppManager.getAppManager().finishActivity();
    }

    void initHomeAsUpEnable(Toolbar toolbar) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                closeBroad();
                finish();

            }
        });
    }

    protected void initToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        initHomeAsUpEnable(toolbar);
    }

    public void initSwipeLayout(SwipeRefreshLayout swipeLayout) {
        swipeLayout.setColorSchemeResources(R.color.colorPrimary,
                R.color.colorPrimaryDark);

        swipeLayout.setSize(SwipeRefreshLayout.LARGE);

        swipeLayout.setProgressBackgroundColor(R.color.colorAccent);
        swipeLayout.setSize(SwipeRefreshLayout.DEFAULT);

        swipeLayout.setProgressViewEndTarget(true, 300);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (getMenuId() != 0) {
            getMenuInflater().inflate(getMenuId(), menu);
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    protected abstract int getContentViewId();

    protected abstract int getMenuId();

    protected abstract void initToolBar();

    protected abstract void setUpViews();

    void closeBroad() {
    }

    boolean isNetvaiable() {
        if (!NetUtils.isConnected(mContext)
                || (NetUtils.isWifi(mContext) && !NetUtils
                .isWifiConnected(mContext))) {
            T.showShort(mContext,
                    mContext.getResources().getString(R.string.check_net));
            return false;
        }
        return true;
    }

    // public void getUserSp() {
    // String key = commSp.getString(Constants.USER_NAME, "");
    // if (key != null) {
    // userSp = SPUtils.getByTag(context, key);
    // }
    // }

    public LoadingHanlder getLoading(int hint) {
        if (handler == null)
            handler = new LoadingHanlder(mContext);
        if (hint != -1)
            handler.setTitleId(hint);
        return handler;
    }

    @Override
    public void start(int type) {

    }

    @Override
    public void success(int type, String json) {

    }

    @Override
    public void failed(String error) {
        Log.e(TAG, "  --------- " + error);
    }

    @Override
    public void finish(int type) {

    }

    public BaseModel validateJson(String json) {
        Log.e("hpt", "json : " + json);
        BaseModel base = JSONObject.parseObject(json, BaseModel.class);
//        if (base.result != Constants.SUCCESS) {
        if (!base.isSuccess()) {
            T.showShort(mContext, base.msg);
            return base;
        }
        return base;
    }

    protected static void JumpMain() {
        Context context = ZBApplication.getInstance();
        Intent it = new Intent(context, LoginActivity.class);
        it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(it);
        // ((Activity) context).finish();
    }
}
